package com.hz.pay.center.controller;

import com.hz.pay.center.exception.BizException;
import com.pay.common.req.PayCloseReq;
import com.pay.common.req.PayQueryReq;
import com.pay.common.resp.NetBankPayRespData;
import com.pay.common.resp.PayCloseData;
import com.pay.common.resp.PayNotifyData;
import com.pay.common.resp.Response;
import com.hz.pay.center.service.IPayService;
import com.pay.common.req.NetBankPayReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 支付请求接口
 */
@RestController
public class PayController {


    @Autowired
    private IPayService payService;

    private Logger logger = LoggerFactory.getLogger(PayController.class);


    /**
     *  查询商户的支付列表
     */
    public Response<List> queryMchPayWays(String mchId){

        //查询商户的支付列表。商户表和商户支付方式表
        return new  Response<List>();
    }




    /**
     * 网银支付接口（异步支付调用此接口）
     * @param bankPayReq
     * @return
     */
    @RequestMapping(value = "pay/create" ,method = RequestMethod.POST)
    public Response<NetBankPayRespData> netBankPay(@RequestBody NetBankPayReq bankPayReq){
        logger.info("##########接收到业务发起的网银支付请求，参数：{}.############",bankPayReq);
        //1.参数校验,空判断

        if (StringUtils.isEmpty(bankPayReq.getMchId())){

            return Response.returnFail("商户id不能为空");
        }
        if (StringUtils.isEmpty(bankPayReq.getOrderAmt())){

            return Response.returnFail("订单金额不能为空");
        }

        if (StringUtils.isEmpty(bankPayReq.getOrderId())){

            return Response.returnFail("订单号不能为空");
        }

        if (StringUtils.isEmpty(bankPayReq.getPayChannel())){

            return Response.returnFail("支付渠道不能为空");
        }

        Response<NetBankPayRespData> response = new Response<NetBankPayRespData>();

        //返回二维码,response
        return payService.doNetBankPay(bankPayReq);

    }


    /**
     * 支付结果查询：业务系统收不到支付系统的支付回调接口
     * @return
     */
    @RequestMapping(value = "pay/query" ,method = RequestMethod.POST)
    public Response<PayNotifyData> payQuery(@RequestBody PayQueryReq payQueryReq){
        logger.info("##########接收到业务发起的支付结果查询请求，参数：{}.############",payQueryReq);

        //1.参数校验，非空判断
        if (StringUtils.isEmpty(payQueryReq.getPaySerialNo())){
            return Response.returnFail("支付流水号不能为空");
        }


        //2.业务处理
        Response<PayNotifyData>  response = new Response<PayNotifyData>();
        try{
            PayNotifyData payNotifyData = payService.queryTrade(payQueryReq);

            response.setData(payNotifyData);
            return response;
        }catch (BizException e){
            logger.error("BizException",e);
            return Response.returnFail(e.getMessage());


        }

    }

    /**
     * 关闭交易
     * @return
     */
    @RequestMapping(value = "pay/close" ,method = RequestMethod.POST)
    public Response<PayCloseData> payClose(@RequestBody PayCloseReq payCloseReq){
        logger.info("##########接收到业务发起的支付关闭请求，参数：{}.############",payCloseReq);

        //参数校验

        Response<PayCloseData>  response = new Response<PayCloseData>();
        try{
            PayCloseData payCloseData = payService.tradeClose(payCloseReq);

            response.setData(payCloseData);
            return response;
        }catch (BizException e){
            logger.error("BizException",e);
            return Response.returnFail(e.getMessage());


        }


    }


}
